package com.supermap.dataservice.cloud.api.user;

import org.apache.ibatis.annotations.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @desc auqa一生不弱于人
 * @date 2019年05月13 11:21
 * @email 1301584033@qq.com
 */
@Mapper
public interface ApiUserMapper {

    //添加用户
    @Insert("insert into sys_api_user(name,password,appkey,appsecret,userId) values(#{name},#{password},#{appkey},#{appsecret},#{userId})")
    Integer addApiUser(@Param("name") String loginName,@Param("password") String password,@Param("appkey") String appkey,@Param("appsecret") String appsecret,@Param("userId") Integer userId);

    @Select("SELECT ID FROM T_USER WHERE LOGINNAME=#{LOGINNAME}")
    Integer getUserId(@Param("LOGINNAME")String loginName);
    //删除用户
    @Delete("update sys_api_user set del_flg=#{del_flg} where id=#{apiUserId}")
    Integer delApiUser(@Param("apiUserId") Integer apiUserId,@Param("del_flg")Integer del_flg);
    //查询用户
    @Select("select t1.id,t1.name as loginName,t2.showName,t2.address,t2.mobile,t1.del_flg from sys_api_user  t1\n" +
            " left join t_user t2 " +
            " on t1.userId=t2.id where  if(isnull(#{apiLoginName}),true,t1.name=#{apiLoginName}) ")
    List<Map<String,Object>>  getUserList(@Param("apiLoginName")String apiLoginName);

    @Select("select count(1) from sys_api_menu t1\n" +
            "inner join sys_api_menu_user t2\n" +
            "on t1.id=t2.apimenuId and t1.url like concat('%',#{apiName},'%') and t2.del_flg=0 and t2.endTime >=  REPLACE(left(now(),10),'-','')\n" +
            "inner join sys_api_user t3\n" +
            "on t2.apiuserId=t3.id\n" +
            "and t3.del_flg=0 and t3.appkey=#{appKey}")
    Integer apiTimeIsNotTake(@Param("apiName") String apiName,@Param("appKey") String appKey);

    @Update("update sys_api_user set appkey=#{appKey},appsecret=#{appSecret} where name=#{name} ")
    Integer upAuth(@Param("appKey")String appKey,@Param("appSecret") String appSecret,@Param("name") String name);

    @Select("select count(1) from sys_api_user where appkey=#{appKey} and appsecret=#{appScrect}")
    Integer  isAppKeyExist(@Param("appKey") String appKey, @Param("appScrect") String appScrect);
    //登录
    @Select("select appkey,appsecret  from sys_api_user where name=#{loginName} and password=#{pwd} and del_flg=0")
    Map<String,String> apiLogin(@Param("loginName") String loginName, @Param("pwd") String pwd);


    @Delete("delete  from sys_api_menu_user where apiuserId=#{apiuserId} and apimenuId in (${dels})")
    Integer delUserMenu(@Param("apiuserId")Integer apiuserId,@Param("dels")String dels);
    //给菜用户分配用户
    @InsertProvider(type = ApiUserMapperSQL.class,method ="putApiUserMenuSQL")
    Integer putApiUserMenu (@Param("apiuserId")Integer apiuserId,@Param("apimenuIds") String apimenuIds,@Param("endTime")String endTime);

    class ApiUserMapperSQL{

        public String putApiUserMenuSQL(Map<String,Object> map){
            String apimenuIds[]=((String) map.get("apimenuIds")).split(",");
            Integer apiuserId=(Integer) map.get("apiuserId");
            String endTime=(String)map.get("endTime");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append("insert ignore  sys_api_menu_user(apimenuId,apiuserId,endTime) values ");
            for(String apimenuId:apimenuIds){
                stringBuilder.append(" ("+Integer.valueOf(apimenuId)+","+apiuserId+",'"+endTime+"'),");
            }
            return  stringBuilder.substring(0,stringBuilder.length()-1);
        }
    }
}
